<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    :footer="null"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="提现申请单号">
          <a-input placeholder="请输入提现申请单号" v-decorator="['withdrawalNumber', {}]" :disabled="isLook"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类型">
          <a-select v-decorator="[ 'type', {} ]" :disabled="isLook"
                    placeholder="类型"
                    allowClear>
            <a-select-option value="1">支付宝</a-select-option>
            <a-select-option value="2">微信</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="支付宝账号" v-show="type == 1">
          <a-input placeholder="请输入支付宝账号" v-decorator="['aliPayAccount', {}]" :disabled="isLook"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="收款姓名" v-show="type == 1">
          <a-input placeholder="请输入收款姓名" v-decorator="['name', {}]" :disabled="isLook"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="提现金额">
          <a-input-number v-decorator="[ 'amount', {}]" :disabled="isLook"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="付款金额">
          <a-input-number v-decorator="[ 'payAmount', {}]" :disabled="isLook"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="手续费">
          <a-input-number v-decorator="[ 'serviceAmount', {}]" :disabled="isLook"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="审核备注">
          <a-input placeholder="请输入审核备注" v-model="remarks"/>
        </a-form-item>
      </a-form>
      <div class="operation" style="text-align: center">
        <a-button @click="review(99)">审核拒绝</a-button>
        <a-button type="primary"
                  @click="review(1)">审核通过
        </a-button>
      </div>
    </a-spin>
  </j-modal>
</template>

<script>
  import {getAction, httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "MedicalUserWithdrawModal",
    data() {
      return {
        title: "操作",
        visible: false,
        isLook: true,
        type: null,
        remarks: null,
        withdrawId: null,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          add: "/user/medicalUserWithdraw/add",
          edit: "/user/medicalUserWithdraw/edit",
          review: "/user/medicalUserWithdraw/withdrawReview",
        },
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.type = null;
        this.remarks = null;
        this.form.resetFields();
        this.model = Object.assign({}, record);
        if (record.id) {
          this.type = record.type;
          this.remarks = record.remarks;
          this.withdrawId = record.id;
        }
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'withdrawalNumber', 'type', 'subType', 'aliPayAccount', 'openid', 'userId', 'status', 'orderNo', 'name', 'amount', 'payAmount', 'serviceAmount', 'payTime', 'paymentSerialNumber', 'remarks', 'paymentRemark', 'delFlag'))
          //时间格式化
          this.form.setFieldsValue({reviewTime: this.model.reviewTime ? moment(this.model.reviewTime) : null})
        });
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.reviewTime = formData.reviewTime ? formData.reviewTime.format('YYYY-MM-DD HH:mm:ss') : null;
            console.log(formData);
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close();
      },
      review(status) {
        this.confirmLoading = true;
        console.info("withdrawId:" + this.withdrawId)
        let params = {
          status: status,
          remarks: this.remarks
        };
        getAction(this.url.review + "/" + this.withdrawId, params).then(res => {
          if (res.success) {
            this.$message.success(res.message);
            this.$emit('ok')
          } else {
            this.$message.warning(res.message);
          }
        }).finally(() => {
          this.confirmLoading = false;
          this.close();
        })
      }
    }
  }
</script>

<style lang="less" scoped>

</style>